package importexport;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.xml.parsers.ParserConfigurationException;

import database.dbObjects.IDBContact.DBContact;
import database.dbObjects.IDBCourse.DBCourse;
import database.dbObjects.IDBEvent.DBEvent;
import database.dbObjects.IDBExam.DBExam;
import database.dbObjects.IDBLesson.DBLesson;
import database.dbObjects.IDBObject.DBObject;
import database.dbObjects.IDBProject.DBProject;
import database.dbObjects.IDBStudent.DBStudent;
import database.dbObjects.IDBTask.DBTask;
import database.dbObjects.IDBTeacher.DBTeacher;

public class Importtests {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/*VCard card = new VCard("test.vcf");
		System.out.println(card.getTitle() + " " + card.getFirstName() + " " + card.getLastName());
		System.out.println(card.getOffice() + "\n" + card.getPhoneNumber() + "\n" + card.getEmail());
		if (card.isValid()) {
			System.out.println("\n ok!!");
		}
		Icalendar ical = new Icalendar("test.ical");
		
		System.out.println(ical.getEvent(0).getStart() + "\n" + ical.getEvent(0).getWarningDate());
		System.out.println("\n" + ical.getEvent(1).getStart() + "\n" + ical.getEvent(1).getWarningDate());
		if (ical.isValid()) {
			System.out.println("ok");
		}
		XmlParser parser = new XmlParser("C:\\Documents and Settings\\slimbo86\\workspace\\dbtests");
		try {
			if (parser.parse()) {
				System.out.println("hoi");
				System.out.println(parser.getCourse(0).getTutorType(2));
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("ok");
		File dir = new File("C:\\Documents and Settings\\slimbo86\\workspace\\dbtests\\porno");
		File file = new File("C:\\Documents and Settings\\slimbo86\\workspace\\dbtests\\hoi.txt");
		File file2 = new File("C:\\Documents and Settings\\slimbo86\\workspace\\dbtests\\hoihoi.txt");
		try {
			copyFile(file, file2);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Class clObj;

	    clObj = file.getClass();
	    System.out.println(clObj.getName());
		DBObject obj = new DBContact();
		System.out.println(obj.getClass().getName());
		obj = new DBCourse();
		System.out.println(obj.getClass().getName());
		obj = new DBEvent();
		System.out.println(obj.getClass().getName());
		obj = new DBExam();
		System.out.println(obj.getClass().getName());
		obj = new DBLesson();
		System.out.println(obj.getClass().getName());
		obj = new DBProject();
		System.out.println(obj.getClass().getName());
		obj = new DBStudent();
		System.out.println(obj.getClass().getName());
		obj = new DBTask();
		System.out.println(obj.getClass().getName());
		obj = new DBTeacher();
		System.out.println(obj.getClass().getName());
		try {
			createZip("C:\\Documents and Settings\\slimbo86\\workspace\\dbtests\\export.zip");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		XmlParser parser = new XmlParser("D:\\Nieuwe map");
		try {
			if (parser.parse()) {
				System.out.println("hoi");
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	static void recursiveDelete(File dirPath) {
	    String [] ls = dirPath.list ();
	    for (int idx = 0; idx < ls.length; idx++) {
	    	File file = new File (dirPath, ls [idx]);
	    	if (file.isDirectory ())
	        recursiveDelete (file);
	    	file.delete ();
	    }
	    dirPath.delete();
	}
	
	public static void copyFile(File in, File out) throws IOException {
    FileChannel inChannel = new FileInputStream(in).getChannel();
    FileChannel outChannel = new FileOutputStream(out).getChannel();
    try {
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } 
    catch (IOException e) {
        throw e;
    }
    finally {
        if (inChannel != null) inChannel.close();
        if (outChannel != null) outChannel.close();
    }
    }
	
	private static void createZip(String path) throws IOException {
		File inFolder=new File("export");
	    File outFolder=new File(path);
	    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFolder)));
	    BufferedInputStream in = null;
	    byte[] data    = new byte[1000];
	    String files[] = inFolder.list();
	    for (int i=0; i<files.length; i++) {
	    	in = new BufferedInputStream(new FileInputStream(inFolder.getPath() + "/" + files[i]), 1000);                  
	    	out.putNextEntry(new ZipEntry(files[i])); 
	    	int count;
	    	while((count = in.read(data,0,1000)) != -1) {
	    		out.write(data, 0, count);
	        }
	    	out.closeEntry();
	    }
	    out.flush();
	    out.close();
	}


}
